package com.alibaba.citrus.cr.unified.inventory.utils;

import com.epoch.app.crunifiedinventory.sdo.enums.AllocateOrderShipTypeEnums;
import com.epoch.app.crunifiedinventory.sdo.enums.TransportModeEnum;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.*;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Arrays;


/**
 * @description:
 * @author: yuhw
 * @date: 2024年01月11日 21:16
 **/
public class ToolUtil {

    public static boolean isTrainOrSea(Integer shipMethod){
        return Arrays.asList(AllocateOrderShipTypeEnums.TRAIN,AllocateOrderShipTypeEnums.SEA)
                .stream().anyMatch(type -> type.getType().equals(shipMethod));
    }

    public static boolean isTrain(Integer shipMethod){
        return AllocateOrderShipTypeEnums.TRAIN.getType().equals(shipMethod);
    }

    public static boolean isSea(Integer shipMethod){
        return AllocateOrderShipTypeEnums.SEA.getType().equals(shipMethod);
    }
    public static boolean isTrainOrSea(String shipMethod){
        return Arrays.asList(TransportModeEnum.RAILWAY_TRANSPORTATION_DISTRIBUTION,
                TransportModeEnum.OCEAN_SHIPPING_DISTRIBUTION).stream()
                .anyMatch(type -> type.getCode().equals(shipMethod) || deleteZeroPrefix(type.getCode()).equals(shipMethod));
    }

    public static String deleteZeroPrefix(String str){
        if (StringUtils.isBlank(str)){
            return null;
        }
        return str.replaceFirst("^0*","");
    }

    public static void addEmptyString(Object object){
        if (object == null){
            return;
        }
        try {
            Class<?> clazz = object.getClass();
            for (Field field : clazz.getDeclaredFields()) {
                field.setAccessible(true);
                Class<?> type = field.getType();
                if (Modifier.isFinal(field.getModifiers()) || type.isPrimitive()){
                    continue;
                }
                if (type == String.class){
                    String value = (String) field.get(object);
                    if (value == null){
                        field.set(object, "");
                    }
                }else if (type == Boolean.class){
                    Boolean value = (Boolean) field.get(object);
                    if (value == null){
                        field.set(object,Boolean.FALSE);
                    }
                }

            }
        }catch (Throwable e){
            e.printStackTrace();
        }
    }

    //TODO 先满足基本使用
    public static void trim(Object target){
        try{
            if (target == null){
                return;
            }
            Class<?> clazz = target.getClass();
            for (;clazz != Object.class ; clazz = clazz.getSuperclass()){
                Field[] fields = clazz.getDeclaredFields();
                for (Field field : fields) {
                    field.setAccessible(true);
                    Class<?> type = field.getType();
                    if (Modifier.isFinal(field.getModifiers()) || type.isPrimitive()){
                        continue;
                    }
                    if (type == String.class){
                        String value = (String) field.get(target);
                        if (value != null && value.length() > 0){
                            field.set(target, value.trim());
                        }
                    }else if (Collection.class.isAssignableFrom(type)){
                        Type genericType = field.getGenericType();
                        if (genericType instanceof ParameterizedType){
                            ParameterizedType parameterizedType = (ParameterizedType) genericType;
                            Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
                            boolean isString = Arrays.asList(actualTypeArguments).stream().anyMatch(t -> t == String.class);
                            if (isString){
                                Collection<String> collection = (Collection<String>) field.get(target);
                                Collection<String> trimCollection = new ArrayList<>();
                                if (collection != null && !collection.isEmpty()){
                                    for (String v : collection) {
                                        if (v != null && v.trim().length() > 0){
                                            trimCollection.add(v.trim());
                                        }
                                    }
                                    collection.clear();
                                    if (!trimCollection.isEmpty()){
                                        collection.addAll(trimCollection);
                                    }
                                }
                            }
                            //TODO
                        }
                    }
                }
            }
        }catch (Throwable throwable){

        }
    }
}
